<?php
/**
 * Created by PhpStorm.
 * User: wangsentao
 * Date: 2018/2/27
 * Time: 下午3:17
 */

namespace app\admin\controller\v1;

use think\Collection;
use app\admin\controller\Fun;
use clt\Leftnav;
use think\Db;

/**
 * @title 后台-权限管理组-V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class Auth extends Base
{

    /*public function addMenu()
    {
        $model = model('app\common\model\SysMenu');
        $model->pid = 5;//父节点
        $model->bar_id = 1;
        $model->sm_name = '删除试题';
        $model->bar_id = 1;
        $model->is_dir = 0;
        $model->is_page = 0;
        $model->is_menu = 1;
        $model->api_url = '';//json_encode(['command'=>'jj','interface'=>'nn']);
        $model->status = 1;
        $model->save();
    }*/

    // /**
    //  * @title 返回权限列表
    //  * @description
    //  * @author 极客Sen
    //  * @url /admin/index/index
    //  * @method POST
    //  *
    //  * @header name:interface require:1 default:Auth desc:接口组(全局)
    //  * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
    //  * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
    //  *
    //  * @param name:command type:string require:1 other:showMenu desc:权限列表
    //  * @return data:[]
    //  * 展示用户权限列表
    //  */
    // public function showMenu()
    // {
    //     $user = model('app\common\Model\SysMenu');
    //     $list = $user
    //         ->where('status',1)
    //         ->field('sm_id,pid,sm_name,api_url,is_dir,is_page,is_menu')
    //         ->select()
    //         ->toArray();

        // //第一步，将分类id作为数组key,并创建children单元
        // foreach($res as $category){
        //     if($category['is_menu'] != 1){
        //         $tree[$category['sm_id']] = $category;
        //         $tree[$category['sm_id']]['children'] = array();
        //     }
        // }

        // //第二部，利用引用，将每个分类添加到父类children数组中，这样一次遍历即可形成树形结构。
        // foreach ($tree as $k=>$item) {
        //     if ($item['pid'] != 0) {
        //         $tree[$item['pid']]['children'][] = &$tree[$k];
        //     }
        // }
    //     $tree = make_tree($list);
    //     return Fun::success($tree);
    // }


    /**
     * @title 管理员列表（后台用户管理列表）
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default:adminList other:adminList desc:管理员列表（后台用户管理列表）
     *
     * @param name:page type:string require:1 default: other: desc:页码
     * @param name:page_size type:string require:1 default: other: desc:每页个数
     * @param name:search type:string require:1 default: other: desc:查询数据
     * @param name:sort_field type:string require:1 default: other: desc:排序字段
     * @param name:sort_type type:string require:1 default: other: desc:0正序,1倒序
     *
     * @return data:[]
     */
    public function adminList()
    {
        $where = [];
        //查询
        if (isset($this->param['search']) && $this->param['search'] != '') {
            $search = $this->param['search'];
            $where['u.realname|u.phone'] = ['like', '%' . $search . '%'];
        }
        $Auth = new \app\common\Model\Auth();
        if (isset($this->param['sort_field']) && $this->param['sort_field'] != '' && in_array($this->param['sort_field'], $Auth->sort_field) && isset($this->param['sort_type'])) {
            $order = [
                $this->param['sort_field'] => $this->sort_type[intval($this->param['sort_type'])]
            ];
        } else {
            $order = ['create_time' => $this->sort_type[1]];
        }
        $list = Db::name('admin_user u')
            ->join('auth_group ag', 'u.group_id = ag.group_id', 'left')
            ->where($where)
            ->order($order)
            ->field('u.uid,u.realname,u.phone,u.status,u.create_time,ag.title,u.group_id')
            ->paginate($this->page_size, false, ['page' => $this->param['page']]);
        if ($list) {
            foreach ($list->items() as $k => $v) {
                $arr = $v;
                $arr['status'] = $Auth->_status[$v['status']];
                $data[] = $arr;
            }
            return Fun::success(['list' => $data, 'total_num' => $list->total()]);
        } else {
            return Fun::error('333333');
        }
    }

    /**
     * @title 用户禁用恢复
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:alterAdminUserStatus desc:用户禁用恢复
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:用户id
     * @param name:status type:string require:1 default: other:0 desc:用户状态:1锁定,0正常
     *
     * @return data:[]
     */
    public function alterAdminUserStatus()
    {
        $ids = explode(',', trim($this->param['ids'], ','));
        $updata = [];

        if (isset($this->param['status']) && in_array(intval($this->param['status']), [0,1])) {
            $updata['status'] = $this->param['status'];
        }
        if (count($updata) && count($ids)) {
            $res = Db::name('admin_user')->where('uid', 'in', $ids)
                ->update($updata);
            if ($res) {
                return Fun::success('操作成功');
            }
        }
        return Fun::error('333332');//TODO 无数据，定义错误类型
    }

    /**
     * @title 可用用户组类型(下拉框使用)
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:adminGroupType desc:可用用户组类型(下拉框使用)
     *
     * @return data:[]
     */
    public function adminGroupType()
    {
        $where = ['status' => 1];

        $list = Db::name('auth_group')
            ->where($where)
            ->field('group_id,title')
            ->select();
        if ($list) {
            return Fun::success($list);
        } else {
            return Fun::error('333333');
        }
    }   

    /**
     * @title 添加新的后台用户
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:addAdminUser desc:添加新的后台用户
     *
     * @param name:realname type:string require:1 default: other: desc:后台用户名称
     * @param name:phone type:string require:1 default: other: desc:后台用户手机号
     * @param name:passwd type:string require:1 default: other: desc:后台用户密码
     * @param name:group_id type:string require:1 default: other: desc:后台用户所属用户组id
     *
     * @return data:[]
     */
    public function addAdminUser()
    {
        $data = [
            'user_type'=>0,
            'realname' => $this->param['realname'],
            'phone' => $this->param['phone'],
            'group_id' => $this->param['group_id'],
            'passwd' => Fun::encryptPwd($this->param['passwd']),
            'status' => 0,
            'acc_id' => GenToken(),
            'create_time' => date('Y-m-d H:i:s',time()),
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        // 启动事务
        Db::startTrans();
        try {
            $res = Db::name('admin_user')
                ->insert($data);
            // 提交事务
            Db::commit();
            return Fun::success('添加成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return Fun::error('333332');
        }
    }

    /**
     * @title 编辑后台用户
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:editAdminUser desc:编辑后台用户
     *
     * @param name:id type:string require:1 default: other:1 desc:后台用户Id
     * @param name:realname type:string require:1 default: other: desc:后台用户名称
     * @param name:phone type:string require:1 default: other: desc:后台用户手机号
     * @param name:passwd type:string require:1 default: other: desc:后台用户密码
     * @param name:group_id type:string require:1 default: other: desc:后台用户所属用户组id
     *
     * @return data:[]
     */
    public function editAdminUser()
    {
        if (empty($this->param['id']) || empty($this->param['realname']) || empty($this->param['group_id'])) {
            return Fun::error('999994');
        }

        $AdminUser = new \app\common\Model\AdminUser();
        $res = $AdminUser->where('uid', $this->param['id'])->find();
        if (!$res) {
            return Fun::error('333333');
        }

        // 启动事务
        Db::startTrans();
        try {
            $res->realname = $this->param['realname'];
            $res->phone = $this->param['phone'];
            $res->group_id = $this->param['group_id'];
            $res->passwd = $this->param['passwd'];
            $res->create_time = date('Y-m-d H:i:s',time());
            $res->save();
            // 提交事务
            Db::commit();
            return Fun::success('修改成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return Fun::error('333332');
        }
    }

    /**
     * @title 用户组列表
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default:adminGroup other:adminGroup desc:用户组列表
     *
     * @param name:page type:string require:1 default: other: desc:页码
     * @param name:page_size type:string require:1 default: other: desc:每页个数
     * @param name:sort_field type:string require:1 default: other: desc:排序字段
     * @param name:sort_type type:string require:1 default: other: desc:0正序,1倒序
     *
     * @return data:[]
     */
    public function adminGroup()
    {
        $AuthGroup = new \app\common\Model\AuthGroup();
        if (isset($this->param['sort_field']) && $this->param['sort_field'] != '' && in_array($this->param['sort_field'], $AuthGroup->user_sort_field) && isset($this->param['sort_type'])) {
            $order = [
                $this->param['sort_field'] => $this->sort_type[intval($this->param['sort_type'])]
            ];
        } else {
            $order = ['create_time' => $this->sort_type[1]];
        }
        $list = Db::name('auth_group')
            ->order($order)
            ->field('group_id,title,create_time,status')
            ->paginate($this->page_size, false, ['page' => $this->param['page']]);
        if ($list) {
            return Fun::success(['list' => $list->items(), 'total_num' => $list->total()]);
        } else {
            return Fun::error('333333');
        }
    }

    /**
     * @title 用户组禁用恢复
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:alterAdminGroupStatus desc:用户组禁用恢复
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:用户组id
     * @param name:status type:string require:1 default: other:0 desc:用户状态:1正常,0禁用
     *
     * @return data:[]
     */
    public function alterAdminGroupStatus()
    {
        $ids = explode(',', trim($this->param['ids'], ','));
        $updata = [];

        if (isset($this->param['status']) && in_array(intval($this->param['status']), [0,1])) {
            $updata['status'] = $this->param['status'];
        }
        if (count($updata) && count($ids)) {
            $res = Db::name('auth_group')->where('group_id', 'in', $ids)
                ->update($updata);
            if ($res) {
                return Fun::success('操作成功');
            }
        }
        return Fun::error('333332');//TODO 无数据，定义错误类型
    }

    /**
     * @title 添加新的后台用户组
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:addAdminGroup desc:添加新的后台用户组
     *
     * @param name:title type:string require:1 default: other: desc:后台用户组名称
     *
     * @return data:[]
     */
    public function addAdminGroup()
    {
        $data = [
            'title' => $this->param['title'],
            'status' => 1,
            'create_time' => date('Y-m-d H:i:s',time()),
        ];
        // 启动事务
        Db::startTrans();
        try {
            $res = Db::name('auth_group')
                ->insert($data);
            // 提交事务
            Db::commit();
            return Fun::success('添加成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return Fun::error('333332');
        }
    }

    /**
     * @title 编辑后台用户组
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:editAdminGroup desc:编辑后台用户组
     *
     * @param name:group_id type:string require:1 default: other: desc:后台用户组ID 
     * @param name:title type:string require:1 default: other: desc:后台用户组名称
     *
     * @return data:[]
     */
    public function editAdminGroup()
    {
        if (empty($this->param['group_id']) || empty($this->param['title'])) {
            return Fun::error('999994');
        }

        $AuthGroup = new \app\common\Model\AuthGroup();
        $res = $AuthGroup->where('group_id', $this->param['group_id'])->find();

        if (!$res) {
            return Fun::error('333333');
        }

        $res = Db::name('auth_group')->where('group_id='.$this->param['group_id'])
            ->update(['title'=>$this->param['title']]);
        if ($res) {
            return Fun::success('操作成功');
        }else{
             return Fun::error('333332');
        }

    }

    /**
     * @title 获得用户组所拥有的权限菜单
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:groupAccess desc:获得用户组所拥有的权限菜单
     *
     * @param name:group_id type:string require:1 default: other: desc:后台用户组ID 
     *
     * @return data:[]
     */
    public function groupAccess()
    {
        if (empty($this->param['group_id'])) {
            return Fun::error('999994');
        }

        $nav = new Leftnav();
        $admin_rule=db('auth_rule')->field('id,pid,title')->order('sort asc')->select();
        $rules = db('auth_group')->where('group_id',$this->param['group_id'])->value('rules');
        $arr = $nav->auth($admin_rule,$pid=0,$rules);
        // $arr[] = array(
        //     "id"=>0,
        //     "pid"=>0,
        //     "title"=>"全部",
        //     "open"=>true
        // );

        return Fun::success(['list' => $arr]);
    }

    /**
     * @title 编辑后台用户组权限菜单
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:groupSetaccess desc:编辑后台用户组权限菜单
     *
     * @param name:group_id type:string require:1 default: other: desc:后台用户组ID 
     * @param name:rules type:string require:1 default: other:10086,10087,10088, desc:权限菜单ID
     *
     * @return data:[]
     */
    public function groupSetaccess()
    {
        if (empty($this->param['group_id']) || empty($this->param['rules'])) {
            return Fun::error('999994');
        }

        $AuthGroup = new \app\common\Model\AuthGroup();
        $res = $AuthGroup->where('group_id', $this->param['group_id'])->find();
        if (!$res) {
            return Fun::error('333333');
        }

        $result = Db::name('auth_group')->where('group_id='.$this->param['group_id'])
            ->update(['rules'=>$this->param['rules']]);
        if ($result) {
            return Fun::success('操作成功');
        }else{
             return Fun::error('333332');
        }

    }

    /**
     * @title 权限管理列表
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default:adminRule other:adminRule desc:权限管理列表
     *
     * @return data:[]
     */
    public function adminRule()
    {
        $nav = new Leftnav();
        $arr = cache('authRuleList');
        if(!$arr){
            $authRule = new \app\common\Model\AuthRule();
            $rule = $authRule::all(function($query){
                $query->order('sort', 'asc');
            });
            $arr = $nav->menu($rule);
            cache('authRuleList', $arr, 3600);
        }

        return Fun::success(['list' => $arr]);

    }

    /**
     * @title 设置权限菜单显示或者隐藏
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:ruleState desc:设置权限菜单显示或者隐藏
     *
     * @param name:id type:string require:1 default: other:10086 desc:权限菜单id
     * @param name:menustatus type:string require:1 default: other:0 desc:用户状态:1正常,0禁用
     *
     * @return data:[]
     */
    public function ruleState()
    {
        $updata = [];

        if (isset($this->param['menustatus']) && in_array(intval($this->param['menustatus']), [0,1])) {
            $updata['menustatus'] = $this->param['menustatus'];
        }
        if (count($updata) && count($this->param['id'])) {
            $res = Db::name('auth_rule')->where('id='.$this->param['id'])
                ->update($updata);
            if ($res) {
                cache('authRule', NULL);
                cache('authRuleList', NULL);
                return Fun::success('操作成功');
            }
        }
        return Fun::error('333332');//TODO 无数据，定义错误类型
    }

    /**
     * @title 设置权限菜单排序
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:ruleOrder desc:设置权限菜单排序
     *
     * @param name:id type:string require:1 default: other:10086 desc:权限菜单id
     * @param name:sort type:string require:1 default: other: desc:排序
     *
     * @return data:[]
     */
    public function ruleOrder()
    {
        $updata = [];

        if (isset($this->param['sort'])) {
            $updata['sort'] = $this->param['sort'];
        }
        if (count($updata) && count($this->param['id'])) {
            $res = Db::name('auth_rule')->where('id='.$this->param['id'])
                ->update($updata);
            if ($res) {
                cache('authRule', NULL);
                cache('authRuleList', NULL);
                return Fun::success('操作成功');
            }
        }
        return Fun::error('333332');//TODO 无数据，定义错误类型
    }

/*    *
     * @title 设置权限是否验证
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:ruleTz desc:设置权限是否验证
     *
     * @param name:id type:string require:1 default: other:10086 desc:权限菜单id
     * @param name:authopen type:string require:1 default: other: desc:1不验证,0验证
     *
     * @return data:[]
     
    public function ruleTz()
    {
        $updata = [];

        if (isset($this->param['authopen'])) {
            $updata['authopen'] = $this->param['authopen'];
        }
        if (count($updata) && count($id)) {
            $res = Db::name('auth_rule')->where('id='.$this->param['id'])
                ->update($updata);
            if ($res) {
                cache('authRule', NULL);
                cache('authRuleList', NULL);
                return Fun::success('操作成功');
            }
        }
        return Fun::error('333332');//TODO 无数据，定义错误类型
    }*/

    /**
     * @title 删除权限菜单
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:command type:string require:1 default: other:ruleDel desc:删除权限菜单
     *
     * @param name:id type:string require:1 default: other:10086 desc:权限菜单id
     *
     * @return data:[]
     */
    public function ruleDel()
    {
        if (empty($this->param['id'])) {
            return Fun::error('999994');
        }
        $AuthRule = new \app\common\Model\AuthRule();
        $AuthRule::destroy(['id'=>$this->param['id']]);
        cache('authRule', NULL);
        cache('authRuleList', NULL);

        return Fun::success('操作成功');
    }

    /**
     * @title 添加新的权限
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:addRule desc:添加新的权限
     *
     * @param name:pid type:string require:1 default: other: desc:父级权限ID(如果添加的为顶级菜单，则传0)
     * @param name:title type:string require:1 default: other: desc:权限名称
     * @param name:href type:string require:1 default: other: desc:控制器/方法,例如Sys/sysList
     * @param name:menustatus type:string require:1 default: other: desc:菜单状态,1开启,0关闭
     * @param name:sort type:string require:1 default: other: desc:排序
     *
     * @return data:[]
     */
    public function addRule()
    {
        $data = [
            'pid' => $this->param['pid'],
            'title' => $this->param['title'],
            'href' => $this->param['href'],
            'menustatus' => $this->param['menustatus'],
            'sort' => $this->param['sort'],
            'addtime' => time(),
        ];
        // 启动事务
        Db::startTrans();
        try {
            $res = Db::name('auth_rule')
                ->insert($data);
            cache('authRule', NULL);
            cache('authRuleList', NULL);
            // 提交事务
            Db::commit();
            return Fun::success('添加成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return Fun::error('333332');
        }
    }

    /**
     * @title 编辑权限
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Auth desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:editRule desc:编辑权限
     *
     * @param name:id type:string require:1 default: other: desc:权限ID
     * @param name:pid type:string require:1 default: other: desc:父级权限ID(如果添加的为顶级菜单，则传0)
     * @param name:title type:string require:1 default: other: desc:权限名称
     * @param name:href type:string require:1 default: other: desc:控制器/方法,例如Sys/sysList
     * @param name:menustatus type:string require:1 default: other: desc:菜单状态,1开启,0关闭
     * @param name:sort type:string require:1 default: other: desc:排序
     *
     * @return data:[]
     */
    public function editRule()
    {
        if (empty($this->param['id'])) {
            return Fun::error('999994');
        }

        $AuthRule = new \app\common\Model\AuthRule();
        $res = $AuthRule->where('id', $this->param['id'])->find();
        if (!$res) {
            return Fun::error('333333');
        }

        $res = Db::name('auth_rule')->where('id='.$this->param['id'])
            ->update(['pid'=>$this->param['pid'],'title'=>$this->param['title'],'href'=>$this->param['href'],'menustatus'=>$this->param['menustatus'],'sort'=>$this->param['sort']]);
        if ($res) {
            cache('authRule', NULL);
            cache('authRuleList', NULL);
            return Fun::success('操作成功');
        }else{
            return Fun::error('333332');
        }
        
    }


}